package com.duan.laugh.email.controller;

import com.duan.laugh.common.core.util.R;
import com.duan.laugh.email.api.pojo.entity.MailRequest;
import com.duan.laugh.email.service.MailService;
import com.duan.laugh.starter.email.EmailAccount;
import com.duan.laugh.starter.email.EmailAccountLocator;
import com.duan.laugh.starter.email.RedisEmailAccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author duanjw
 */
@RestController
@RequestMapping("mail")
public class MailController {

    @Autowired
    private MailService mailService;

    /**
     * 发送邮件
     *
     * @param mailRequest
     * @return
     */
    @PostMapping
    public R sendMail(@RequestBody MailRequest mailRequest) {
        return R.ok(mailService.sendMail(mailRequest));
    }

    @Autowired
    private EmailAccountLocator emailAccountLocator;

    @Autowired
    private RedisEmailAccountRepository redisEmailAccountDefinitionRepository;
    @GetMapping("/{tenantId}")
    public R test(@PathVariable String tenantId){
//        redisEmailAccountDefinitionRepository.add(emailAccountLocator.getEmailAccount("123").setId("456"));
        final List<EmailAccount> emailAccounts = emailAccountLocator.getEmailAccounts();
        final EmailAccount emailAccount = emailAccountLocator.getEmailAccount("123");
//        MailUtil.send(emailAccount, "554343346@qq.com","收到","是否收到邮件",false);
        final MailRequest mailRequest = new MailRequest();
        mailRequest.setSubject("会议通知");
        mailRequest.setContent("会议通知,今天开会");
        mailRequest.setRec("554343346@qq.com");
        mailRequest.setTenantId(tenantId);
        mailService.sendMail(mailRequest);
        return R.ok(null);
    }
}
